#!/usr/bin/python
# -*- coding:utf-8 -*-

'''
Created on 6 juin 2014

@author: Alexandre Neuville
'''


from logging import DEBUG
from unittest import main, TestCase

# Tag UnusedImport nécessaire pour 'eval()' dans testGetInformations
from persistence import Database, ConnectionInformation, LoggerFactory  # @UnusedImport


class TestDatabase(TestCase):
    @classmethod
    def setUpClass(cls):
        LoggerFactory.configure(DEBUG,
                "%(asctime)s :: %(levelname)s :: %(name)s.%(funcName)s(%(lineno)d) :: %(message)s")

    def setUp(self):
        self.dsn = "SPW1GW58"
        self.logger = LoggerFactory.getStdLogger(self)

    def testConnection(self):
        with Database(self.dsn) as db:
            self.assertTrue(db._isConnected(), "Connection not connected")
        self.assertFalse(db._isConnected(), "Connection always connected")

    def testGetInformations(self):
        with Database(self.dsn) as db:
            infos = db.getDatabaseInformations()
            self.assertIsNotNone(infos, "Informations not retrieved")

            if len(infos) > 0:
                self.assertEqual(eval(repr(infos[0])), infos[0], "Representation false : %r" % infos[0])
                self.logger.debug("First connection information : %s", infos[0])


if __name__ == '__main__':
    main()
